package org.geektimes.projects.user.web.controller;


import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.service.UserService;
import org.geektimes.projects.user.service.impl.UserServiceImpl;
import org.geektimes.web.mvc.controller.Controller;
import org.geektimes.web.mvc.controller.PageController;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.logging.Logger;

/**
 * 用户注册
 */
@Path("/user")
public class UserRegisterController implements PageController {
    private final Logger logger = Logger.getLogger(this.getClass().getName());

    @Resource(name = "bean/UserService")
    private UserService userService;

    /**
     * 用户注册
     * @param request
     * @param response
     * @return
     * @throws Throwable
     */
    @POST
    @Path("/register")
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Throwable {
        String name = request.getParameter("username");
        String password = request.getParameter("pwd");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        user.setEmail(email);
        user.setPhoneNumber(phone);
        boolean register = userService.register(user);
        return register?"registerSuccess.jsp":"registerFailure.jsp";
    }
}
